import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    temp: {},
    // 当前授权用户的 openid
    openid: uni.getStorageSync('openid') || '',
    // 当前登录用户的 token
    token: uni.getStorageSync('token') || '',
    // 用户信息
    userInfo: uni.getStorageSync('userInfo') || {},
    // 当前角色类型 staff=员工 customer=客户
    roleType: uni.getStorageSync('roleType'),
    // 员工角色 shengchan=生产端 wuliu=物流端
    staffRole: '',
    // 员工信息
    staffInfo: uni.getStorageSync('staffInfo') || {},
  },
  mutations: {
    SET_TEMP(state, payload) {
      state.temp = payload
    },
    SET_OPENID(state, payload) {
      uni.setStorageSync('openid', payload)
      state.openid = payload
    },
    SET_TOKEN(state, payload) {
      uni.setStorageSync('token', payload)
      state.token = payload
    },
    SET_USER_INFO(state, payload) {
      uni.setStorageSync('userInfo', payload)
      state.userInfo = payload
    },
    SET_ROLE_TYPE(state, payload) {
      uni.setStorageSync('roleType', payload)
      state.roleType = payload
    },
    SET_STAFF_ROLE(state, payload) {
      state.staffRole = payload
    },
    SET_STAFF_INFO(state, payload) {
      uni.setStorageSync('staffInfo', payload)
      state.staffInfo = payload
    },
  },
  actions: {
    LogOut(context) {
      context.commit('SET_OPENID', '')
      context.commit('SET_TOKEN', '')
      context.commit('SET_ROLE_TYPE', '')
      context.commit('SET_STAFF_ROLE', '')
      context.commit('SET_STAFF_INFO', {})
      context.commit('SET_USER_INFO', {})
      uni.removeStorageSync('openid')
      uni.removeStorageSync('token')
      uni.removeStorageSync('staffInfo')
      uni.removeStorageSync('userInfo')
      uni.removeStorageSync('roleType')
	  
    },
  },
})

export default store
